package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.jaredrummler.materialspinner.MaterialSpinner
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.*
import com.wlzg.collectiontool.util.MConfigUtil
import kotlinx.android.synthetic.main.guanxian_dia.view.*
import kotlinx.android.synthetic.main.guanxian_dia.view.baocun
import kotlinx.android.synthetic.main.guanxian_dia.view.close
import kotlinx.android.synthetic.main.guanxian_dia.view.glgc
import kotlinx.android.synthetic.main.guanxian_dia.view.glwz
import kotlinx.android.synthetic.main.guanxian_dia.view.glxm
import kotlinx.android.synthetic.main.guanxian_dia.view.ifquexian
import kotlinx.android.synthetic.main.guanxian_dia.view.ssleixing
import kotlinx.android.synthetic.main.guanxian_dia.view.zengbu
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class GuanxianDia(var mContext: Context, var mFragment: InvestMapFragment, var mgx: PsGuanxian? = null) :
    CenterPopupView(mContext),
    View.OnClickListener, MaterialSpinner.OnItemSelectedListener<Any> {
    var glXmId = 0L;
    var glWzId = 0L;
    var ifxq = false
    var glGcId = 0L;
    var proList: ArrayList<PsXiangmu?>? = ArrayList()
    var gcList: ArrayList<PsGongcheng?>? = ArrayList()
    var wzList: ArrayList<Weizhi?>? = ArrayList()
    var gdczPosition = 0
    var gdlxPosition = 0
    var jcfsPosiyion = 0
    var jcfxPosition = 0
    var ifqx = 0
    var ifhunjie = 0

    override fun getImplLayoutId(): Int {
        return R.layout.guanxian_dia
    }

    override fun onCreate() {
        super.onCreate()
        close.setOnClickListener(this)
        shanchu.setOnClickListener(this)
        quxiao.setOnClickListener(this)
        baocun.setOnClickListener(this)
        initSpinner()
        initzb()
        mgx?.let {
        initDdata(it)
        }


    }
    private fun  initzb(){
        val qp=mFragment.qidianPoint
        val zp=mFragment.zdianPoint
        if(qp!=null&&zp!=null ){
            qdx.setText(qp.x.toString())
            qdy.setText(qp.y.toString())
            zdx.setText(zp.x.toString())
            zdy.setText(zp.y.toString())
            qsdh.setText(mFragment.qidianBh)
            zzdh.setText(mFragment.zdianBh)
        }
    }
    private fun initDdata(gx:PsGuanxian){
        initGlxm(gx)
        qsdh.setText(gx.inJuncid)
        zzdh.setText(gx.outJuncid)
        gdcz.selectedIndex=gx.material
        jcff.selectedIndex=gx.checkMethod
        jcfx.selectedIndex=gx.checkdirect
        ifquexian.isChecked= gx.defects != 0
        gdzj.setText(gx.shapeData)
        jccd.setText(gx.pipeLengt.toString())
        ifhj.isChecked=gx.rainsewami!=0
        qdsm.setText(gx.inJuncidMs)
        zdsm.setText(gx.outJuncidMs)
        zdx.setText(gx.endx.toString())
        qjdmgc.setText(gx.starsurfac.toString())
        zjdmgc.setText(gx.endsurface.toString())
        zdy.setText(gx.endy1.toString())
        qdx.setText(gx.startx1.toString())
        qdy.setText(gx.starty1.toString())
        jcdate.setText(gx.checkDate)
        beizhu.setText(gx.remark)
        zengbu.setText(gx.remark2)


    }
    private fun saveCommon(t: BaseGuandian) {
        t.glxmId = glXmId
        t.glgcId = glGcId
        t.glwzId = glWzId
        t.ifxq = ifxq
    }


    private fun initGlxm(t: BaseGuandian) {
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            var xm: PsXiangmu?
            var gc: PsGongcheng?
            var wz: Weizhi?
            withContext(Dispatchers.IO) {
                proList =
                    AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                gcList = AppDatabase.getInstance().userDao()
                    .getGcList() as ArrayList<PsGongcheng?>
                xm = AppDatabase.getInstance().userDao().getXmListbyId(t.glxmId)
                gc = AppDatabase.getInstance().userDao().getGcListByid(t.glgcId)
                if (t.ifxq) {
                    val xq = AppDatabase.getInstance().userDao().getxqbyId(t.glwzId)
                    wz = Weizhi()
                    wz?.id = xq?.xiaoquId
                    wz?.name = xq?.villageName

                } else {
                    val dl = AppDatabase.getInstance().userDao().getDlbyId(t.glwzId)
                    wz = Weizhi()
                    wz?.id = dl?.daoluId
                    wz?.name = dl?.roadName
                }
                val xqlist = AppDatabase.getInstance().userDao().getxqList()
                val dllist = AppDatabase.getInstance().userDao().getdlList()
                wzList?.clear()
                xqlist?.let {
                    for (xq in it) {
                        val wz = Weizhi()
                        wz.id = xq?.xiaoquId
                        wz.name = xq?.villageName
                        wz.ifxq = true
                        wzList?.add(wz)
                    }
                }
                dllist?.let {
                    for (dl in it) {
                        val wz = Weizhi()
                        wz.id = dl?.daoluId
                        wz.name = dl?.roadName
                        wz.ifxq = false
                        wzList?.add(wz)
                    }
                }

            }
            if (proList?.size!! > 0) {
                glxm.setItems(proList as ArrayList<PsXiangmu?>)
                glxm.setOnItemSelectedListener(this@GuanxianDia)
                glxm.selectedIndex = proList?.indexOf(xm)!!
            }
            if (gcList?.size!! > 0) {
                glgc.setItems(gcList as ArrayList<PsGongcheng?>)
                glgc.setOnItemSelectedListener(this@GuanxianDia)
                glgc.selectedIndex = gcList?.indexOf(gc)!!
            }
            if (wzList?.size!! > 0) {
                glwz.setItems(wzList as ArrayList<Weizhi>)
                glwz.setOnItemSelectedListener(this@GuanxianDia)
                glwz.selectedIndex = wzList?.indexOf(wz)!!
            }
        }
    }

    private fun initSpinner() {
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {  proList = AppDatabase.getInstance().userDao().getXmList() as ArrayList<PsXiangmu?>
                gcList = AppDatabase.getInstance().userDao().getGcList() as ArrayList<PsGongcheng?>
                val xqlist = AppDatabase.getInstance().userDao().getxqList()
                val dllist = AppDatabase.getInstance().userDao().getdlList()
                wzList?.clear()
                xqlist?.let {
                    for (xq in it) {
                        val wz = Weizhi()
                        wz.id = xq?.xiaoquId
                        wz.name = xq?.villageName
                        wz.ifxq = true
                        wzList?.add(wz)
                    }
                }
                dllist?.let {
                    for (dl in it) {
                        val wz = Weizhi()
                        wz.id = dl?.daoluId
                        wz.name = dl?.roadName
                        wz.ifxq = false
                        wzList?.add(wz)
                    }
                }
            }
            if (proList?.size!! > 0) {
                glxm.setItems(proList as ArrayList<PsXiangmu?>)
                glXmId = proList?.get(0)?.xiangmuId!!
            }
            glxm.setOnItemSelectedListener(this@GuanxianDia)
            if (gcList?.size!! > 0) {
                glgc.setItems(gcList as ArrayList<PsGongcheng?>)
                glGcId = gcList?.get(0)?.gcId!!
            }
            glgc.setOnItemSelectedListener(this@GuanxianDia)
            if (wzList?.size!! > 0) {
                glwz.setItems(wzList as ArrayList<Weizhi?>)
                glWzId= wzList?.get(0)?.id!!
            }
            glwz.setOnItemSelectedListener(this@GuanxianDia)




            ifquexian.setOnCheckedChangeListener { view, isChecked ->
                ifqx = if (isChecked) 1 else 0
            }
            ifhj.setOnCheckedChangeListener { view, isChecked ->
                ifhunjie = if (isChecked) 1 else 0
            }

        }

        ssleixing.setItems("排水管")
        gdcz.setItems(MConfigUtil.sysAllDictItems?.material as ArrayList)
        gdcz.setOnItemSelectedListener(this)
        gdlx.setItems(MConfigUtil.sysAllDictItems?.pipe_categ as ArrayList)
        gdlx.setOnItemSelectedListener(this)
        jcff.setItems(MConfigUtil.sysAllDictItems?.check_method as ArrayList)
        jcff.setOnClickListener(this)
        jcfx.setItems("顺流", "逆流")
        jcfx.setOnItemSelectedListener(this)
    }

    private fun getText():PsGuanxian {
        var gx = if (mgx != null) {
            mgx as PsGuanxian
        } else {
            PsGuanxian()
        }
        saveCommon(gx)
        gx.inJuncid = qsdh.text.toString()
        gx.outJuncid = zzdh.text.toString()
        gx.material = gdczPosition
        gx.pipeCateg = gdczPosition
        gx.checkMethod = jcfsPosiyion
        gx.checkdirect = jcfxPosition
        gx.defects = ifqx
        gx.shapeData = gdzj.text.toString()
        try {
            gx.pipeLengt = jccd.text.toString().toInt()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        gx.rainsewami = ifhunjie
        gx.inJuncidMs = qdsm.text.toString()
        gx.outJuncidMs = zdsm.text.toString()
        try {
            gx.endx1 = zdx.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        try {
            gx.starsurfac = qjdmgc.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        try {
            gx.endsurface = zjdmgc.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        try {
        gx.endy1=zdy.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        try {
        gx.startx1=qdx.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        try {
            gx.starty1=qdy.text.toString().toDouble()
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
        gx.checkDate=jcdate.text.toString()
        gx.remark=beizhu.text.toString()
        gx.remark2=zengbu.text.toString()
        return gx
    }


    override fun onClick(p0: View?) {
        when (p0?.id) {

            R.id.close -> {
                if(mgx==null){
                mFragment.ondeleteGuanxin(null)}
                dismiss()
            }
            R.id.quxiao -> {
                if(mgx==null){
                    mFragment.ondeleteGuanxin(null)}
                dismiss()
            }
            R.id.baocun -> {
                mFragment.onsaveLine(getText())
                dismiss()
            }
            R.id.shanchu -> {
                mFragment.ondeleteGuanxin(mgx)
                dismiss()
            }
        }
    }

    override fun onItemSelected(view: MaterialSpinner?, position: Int, id: Long, item: Any?) {
        when (view?.id) {
            R.id.glxm -> {
                Log.d("thhhhsss", "sss")
                item as PsXiangmu
                glXmId = item.xiangmuId!!

            }
            R.id.glgc -> {
                item as PsGongcheng
                glGcId = item.gcId!!
                Log.d("YYHH", "id:" + glGcId)

            }
            R.id.glwz -> {
                item as Weizhi
                Log.d("sjjsdfgh","skskk")
                glWzId = item.id!!
                ifxq = item.ifxq

            }
            R.id.gdcz -> {
                gdczPosition = position
            }
            R.id.gdlx -> {
                gdlxPosition = position
            }
            R.id.jcff -> {
                jcfsPosiyion = position
            }
            R.id.jcfx -> {
                jcfxPosition = position
            }
        }
    }
}